<%@ page language="java" contentType="text/html; charset=UTF-8"
	import="java.util.*,com.cap.bts.crmdp.dealer.meal.domain.*"
	pageEncoding="UTF-8"%>

<%
    MealModel mealModel = (MealModel) request.getAttribute("mealModel");
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>套餐修改</title>
<%@include file="../include/include.jsp"%>
<link rel="stylesheet" type="text/css" href="${csspath }/public.css" />
<script type="text/javascript" src="${jspath}/jquery/jquery-2.1.3.js"></script>
<link rel="stylesheet" type="text/css"
	href="${bootstrapPath}/css/bootstrap.css" />
<script type="text/javascript" src="${bootstrapPath}/js/bootstrap.js"></script>
<script type="text/javascript"
	src="${bootstrapPath}/js/bootstrap3-validation.js"></script>

<script type="text/javascript" src="${layerPath}/layer.js"></script>
</head>
<style>
.hr {
	text-align: center;
	width: 100%;
	background-color: #0071C1;
	height: 1px;
	margin-bottom: 10px;
}

.rowDiv {
	width: 90%;
}

.formDiv {
	float: left;
	width: 50%;
}

.textarea {
	width: 700px;
	height: 100px;
	max-width: 700px;
	max-height: 100px;
	resize: none;
}

.btnWidth {
	width: 90px;
}
</style>

<script type="text/javascript">
	$(function() {
		$("#mealForm").validation({
			icon : true
		});
		//返回
		$("#returnBtn").click(
				function() {
					$("#search_form").attr('action',
							'${webcontext}/crmdp/dealer/meal/getMealList');
					$("#search_form").submit();
				});

		//保存
		$("#saveBtn").click(
				function() {
					if ($("#mealForm").valid(this, '内容出错') == false) {
						return false;
					}
					var radio = document.getElementsByName("isPrepaymentCard");
					var isPrepaymentCard = null;
					for ( var i = 0; i < radio.length; i++) {
						if (radio[i].checked == true) {
							isPrepaymentCard = radio[i].value;
							break;
						}
					}
					var sumAmount = $("#sumAmount").val();
					var price = $("#price").val();
					var priceBak = $("#priceBak").val();
					if (isPrepaymentCard == 0 && price != sumAmount) {//不是预付费
						if (price != priceBak) {
							jConfirm("套餐行总价格为" + sumAmount + "元，您修改的价格为"
									+ price + "元，建议零售价是否修改为" + price + "元？",
									"确定", function(r) {
										if (!r) {
											price = sumAmount;
										}
										editPackage(isPrepaymentCard, price);
									});
						} else {
							editPackage(isPrepaymentCard, price);
						}
					} else {
						editPackage(isPrepaymentCard, price);
					}
				});

		//是预付费，不允许修改套餐价格;不是预付费，可修改价格
		$(":radio").click(function() {
			var radio = document.getElementsByName("isPrepaymentCard");
			var isPrepaymentCard = null;
			for ( var i = 0; i < radio.length; i++) {
				if (radio[i].checked == true) {
					isPrepaymentCard = radio[i].value;
					if (isPrepaymentCard == "0") {
						$("#price").attr("disabled", false);
					} else if (isPrepaymentCard == "1") {
						$("#price").attr("disabled", "disabled");
					}
					break;
				}
			}
		});
	});

	function editPackage(isPrepaymentCard, price) {
		$
				.ajax({
					url : '${webcontext}/crmdp/dealer/meal/updateMealInfo',
					type : 'post',
					data : {
						'id' : $("#id").val(),
						'code' : $("#code").val(),
						'name' : $("#name").val(),
						'cardDefId' : $("#cardDefId").val(),
						'status' : $("#status").val(),
						'description' : $("#description").val(),
						'isPrepaymentCard' : isPrepaymentCard,
						'numberSequence' : $("#numberSequence").val(),
						'price' : price,
						'unitOfPeriod' : $("#unitOfPeriod").val(),
						'validPeriod' : $("#validPeriod").val(),
						'memberBenfit' : $("#memberBenfit").val(),
						'cardDefId' : $("#cardDefId").val()
					},
					beforeSend: function(){ 
		  					layer.load(1);
		  				},
					success : function(data) {
						layer.closeAll('loading');
						if (data.code == true) {
							jAlert(
									data.msg,
									"提示",
									function() {
										$("#search_form")
												.attr('action',
														'${webcontext}/crmdp/dealer/meal/toEditMealPage');
										$("#search_form").submit();
									});
						} else if (data.code == false) {
							jAlert(data.msg, "提示");
						}
					},
					error : function() {
						jAlert("服务器错误，请联系管理员", "提示");
					},
				});
	}

	//分页的查询
	function search() {
		document.getElementById("search_form").submit();
	}

	//删除操作
	function removeMealLine(id) {
		//询问框
		jConfirm(
				"确定要删除数据吗？",
				"确认",
				function(r) {
					if (r) {
						$
								.ajax({
									url : '${webcontext}/crmdp/dealer/meal/removeMealLineInfo',
									type : 'post',
									data : {
										'id' : id,
										'packageDefId' : $("#id").val()
									},
									beforeSend: function(){ 
					  					layer.load(1);
					  				},
									success : function(data) {
										layer.closeAll('loading');
										if (data.code == true) {
											jAlert(
													data.msg,
													"提示",
													function() {
														$("#search_form")
																.attr('action',
																		'${webcontext}/crmdp/dealer/meal/toEditMealPage');
														$("#search_form")
																.submit();
													});
										} else if (data.code == false) {
											jAlert(data.msg, "提示");
										}
									},
									error : function() {
										jAlert("服务器错误，请联系管理员", "提示");
									},
								});
					}
				});

	}

	//套餐详细信息修改
	function editMealLineInfo(id) {
		var qName = $("#qName").val();
		var qCode = $("#qCode").val();
		var qStatus = $("#qStatus").val();
		var pager = $("#pager").val();
		layer.open({
			type : 2,
			title : '项目修改',
			shadeClose : false,
			closeBtn : false,
			shade : 0.1,
			offset : [ '16%', '28%' ],
			area : [ '500px', '400px' ],
			content : '${webcontext}/crmdp/dealer/meal/toMealLineInfo?id=' + id
					+ "&qName=" + qName + "&qCode=" + qCode + "&qStatus="
					+ qStatus + "&pager.currentPage=" + pager
		});
	}

	//新增套餐详细
	function insertRow() {
		var id = $("#id").val();
		var qName = $("#qName").val();
		var qCode = $("#qCode").val();
		var qStatus = $("#qStatus").val();
		var pager = $("#pager").val();
		layer
				.open({
					type : 2,
					title : '项目新增',
					shadeClose : false,
					closeBtn : false,
					shade : 0.1,
					offset : [ '16%', '28%' ],
					area : [ '500px', '400px' ],
					content : '${webcontext}/crmdp/dealer/meal/saveMealLineDetails?packageDefId='
							+ id
							+ "&qName="
							+ qName
							+ "&qCode="
							+ qCode
							+ "&qStatus="
							+ qStatus
							+ "&pager.currentPage="
							+ pager
				});
	}

	window.onload = function() {
		var cardDefId = '${mealModel.cardDefId}';
		if (cardDefId != null && cardDefId != "") {
			$("#cardDefId").val(cardDefId);
		}

		var unitOfPeriod = '${mealModel.unitOfPeriod}';
		if (unitOfPeriod != null && unitOfPeriod != "") {
			$("#unitOfPeriod").val(unitOfPeriod);
		}
		var isPrepaymentCard = '${mealModel.isPrepaymentCard}';
		if (isPrepaymentCard != null && isPrepaymentCard != "") {
			if (isPrepaymentCard == 1) {//是预付费
				$("input[name='isPrepaymentCard'][value=1]").attr("checked",
						'true');
				$("#price").attr("disabled", "disabled");
			} else if (isPrepaymentCard == 0) {//不是预付费
				$("input[name='isPrepaymentCard'][value=0]").attr("checked",
						'true');
				$("#price").attr("disabled", false);
			}
		}

		//当套餐状态为有效的时候，则不能修改任何数据
		var status = '${mealModel.status}';
		if (status != null && status != "") {
			$("#status").val(status);
			if (status == "1") {
				$("form[id='mealForm'] :text").attr("disabled", "disabled");
				$("form[id='mealForm'] :radio").attr("disabled", "disabled");
				$("#cardDefId").attr("disabled", "disabled");
				$("#unitOfPeriod").attr("disabled", "disabled");
				$("#memberBenfit").attr("disabled", "disabled");
				$("#saveBtn").attr("disabled", "disabled");
				$("#copyBtn").attr("disabled", "disabled");
				$("#insertRowBtn").attr("disabled", "disabled");
			}
		}
	};

	//入会礼管理
	function giftManage() {
		var id = $("#id").val();
		var status = '${mealModel.status}';
		var temp = false;
		if (status == '1') {
			temp = true;
		}
		var qName = $("#qName").val();
		var qCode = $("#qCode").val();
		var qStatus = $("#qStatus").val();
		var pager = $("#pager").val();
		window.location = '${webcontext}/crmdp/dealer/meal/toMemberGiftPage?id='
				+ id
				+ "&temp="
				+ temp
				+ "&qName="
				+ qName
				+ "&qCode="
				+ qCode
				+ "&qStatus=" + qStatus + "&pager.currentPage=" + pager;
	}

	//复制功能
	function copyMeal() {
		var id = $("#id").val();
		var qName = $("#qName").val();
		var qCode = $("#qCode").val();
		var qStatus = $("#qStatus").val();
		var pager = $("#pager").val();
		layer.open({
			type : 2,
			title : '套餐复制',
			shadeClose : false,
			closeBtn : false,
			shade : 0.1,
			offset : [ '16%', '28%' ],
			area : [ '520px', '200px' ],
			content : '${webcontext}/crmdp/dealer/meal/toCopyMealPage?id=' + id
					+ "&qName=" + qName + "&qCode=" + qCode + "&qStatus="
					+ qStatus + "&pager.currentPage=" + pager
		});
	}

	//适用范围
	function packageEligibilityManage() {
		var status = '${mealModel.status}';
		var temp = false;
		if (status == '1') {
			temp = true;
		}
		var id = $("#id").val();
		var qName = $("#qName").val();
		var qCode = $("#qCode").val();
		var qStatus = $("#qStatus").val();
		var pager = $("#pager").val();
		layer
				.open({
					type : 2,
					title : '适用范围',
					shadeClose : false,
					closeBtn : false,
					scrollbar:true,
					shade : 0.1,
					offset : [ '16%', '28%' ],
					area : [ '600px', '400px' ],
					content : '${webcontext}/crmdp/dealer/meal/toPackageEligibility?packageDefId='
							+ id
							+ "&qName="
							+ qName
							+ "&qCode="
							+ qCode
							+ "&qStatus="
							+ qStatus
							+ "&pager.currentPage="
							+ pager + "&temp=" + temp
				});
	}
</script>
<body>
	<div id="content" style="height: 950px;">
		<div style="color: #025D7B; height: 26px; line-height: 26px;">
			<span>套餐定义</span> <span>&gt;</span> <span>套餐修改</span>
		</div>
		<div class="hr"></div>
		<form:form id="search_form"
			action="${webcontext}/crmdp/dealer/meal/toEditMealPage"
			class="form-horizontal" method="post" modelAttribute="dto"
			role="form">
			<input type="text" class="hidden" name="qName" id="qName"
				value="${qName}">
			<input type="text" class="hidden" name="qCode" id="qCode"
				value="${qCode}">
			<input type="text" class="hidden" name="qStatus" id="qStatus"
				value="${qStatus}">
			<input type="text" class="hidden" id="pager" name="pager.currentPage"
				value="${currentPage}">
			<input type="text" class="hidden" id="id" name="id"
				value="${mealModel.id}" />
			<div class="row rowDiv">
				<div class="form-group formDiv">
					<label class="col-sm-3 control-label" for="code">套餐编号</label>
					<div class="col-sm-6">
						<input class="form-control input-sm" id="code" name="code"
							type="text" value="${mealModel.code}" disabled
							check-type="required" required-message="请填写套餐编号" />
					</div>
				</div>
				<div class="form-group formDiv">
					<label class="col-sm-3 control-label" for="name">套餐名称</label>
					<div class="col-sm-6">
						<input class="form-control input-sm" id="name" name="name"
							value="${mealModel.name}" type="text" check-type="required"
							required-message="请填写套餐名称" />
					</div>
				</div>
			</div>
			<div class="row rowDiv">
				<div class="form-group formDiv">
					<label class="col-sm-3 control-label" for="cardDefId">卡片类型</label>
					<div class="col-sm-6">
						<select id="cardDefId" name="cardDefId"
							value="${mealModel.cardDefId}" class="form-control input-sm">
							<core:forEach items="${cardList}" var="list">
								<option value="${list.id}">${list.name}</option>
							</core:forEach>
						</select>
					</div>
				</div>
				<div class="form-group formDiv">
					<label class="col-sm-3 control-label" for="status">状态</label>
					<div class="col-sm-6">
						<select id="status" name="status" value="${mealModel.status}"
							class="form-control input-sm" disabled>
							<option value="1">有效</option>
							<option value="0">未启用</option>
							<option value="2">停用</option>
						</select>
					</div>
				</div>
			</div>
			<div class="row rowDiv">
				<div class="form-group formDiv">
					<label class="col-sm-3 control-label" for="description">说明</label>
					<div class="col-sm-7">
						<input class="form-control input-sm" id="description"
							value="${mealModel.description}" name="description" type="text"
							check-type="required" required-message="请填写说明" />
					</div>
				</div>
			</div>
			<div class="row rowDiv">
				<div class="form-group formDiv">
					<label class="col-sm-3 control-label" for="isPrepaymentCard">是否预付费</label>
					<div class="col-sm-6">
						<label class="radio-inline"> <input type="radio"
							name="isPrepaymentCard" id="isPrepaymentCard1" value="1">是
						</label> <label class="radio-inline"> <input type="radio"
							name="isPrepaymentCard" id="isPrepaymentCard2" value="0" checked>否
						</label>
					</div>
				</div>
				<div class="form-group formDiv">
					<label class="col-sm-3 control-label" for="numberSequence">购买次数</label>
					<div class="col-sm-6">
						<input class="form-control input-sm text-right"
							id="numberSequence" value="${mealModel.numberSequence}"
							name="numberSequence" type="text" check-type="number"
							required-message="请填写购买次数" />
					</div>
				</div>
			</div>
			<div class="row rowDiv">
				<div class="form-group formDiv">
					<label class="col-sm-3 control-label" for="price">建议零售价</label>
					<div class="col-sm-6">
						<input class="form-control input-sm text-right" id="price"
							name="price" value="${mealModel.price}" type="text" /> <input
							class="hidden" id="sumAmount" name="sumAmount"
							value="${sumAmount}" type="text" /> <input class="hidden"
							id="priceBak" name="priceBak" value="${mealModel.price}"
							type="text" />
					</div>
				</div>
				<div class="form-group formDiv">
					<label class="col-sm-3 control-label" for="validPeriod">有效期</label>
					<div class="col-sm-3">
						<input class="form-control input-sm text-right" id="validPeriod"
							name="validPeriod" value="${mealModel.validPeriod}" type="text" />
					</div>
					<div class="col-sm-3">
						<select id="unitOfPeriod" name="unitOfPeriod"
							value="${mealModel.unitOfPeriod}" class="form-control input-sm">
							<option value="Y">年</option>
							<option value="M">月</option>
							<option value="D">天</option>
						</select>
					</div>
				</div>
				<div class="form-group formDiv">
					<label class="col-sm-3 control-label" for="memberBenfit">会员福利</label>
					<div class="col-sm-3">
						<textarea class="form-control textarea" rows="3" id="memberBenfit"
							name="memberBenfit" maxLength="2000">${mealModel.memberBenfit}</textarea>
					</div>
				</div>
			</div>

			<div style="margin-left: 300px;">
				<button type="button" class="btn btn-success btn-sm btnWidth"
					id="saveBtn">
					<i class="glyphicon glyphicon-floppy-saved" style="color: #FFFFFF"></i>&nbsp;保
					存
				</button>
				<button type="button" class="btn btn-success btn-sm btnWidth"
					id="giftBtn" onclick="giftManage()">
					<i class="glyphicon glyphicon-gift" style="color: #FFFFFF"></i>入会礼管理
				</button>
				<button type="button" class="btn btn-success btn-sm btnWidth"
					id="giftBtn" onclick="packageEligibilityManage()">
					<i class="glyphicon glyphicon-globe" style="color: #FFFFFF"></i>适用范围
				</button>
				<button type="button" class="btn btn-success btn-sm btnWidth"
					id="copyBtn" onclick="copyMeal()">
					<i class="glyphicon glyphicon-duplicate" style="color: #FFFFFF"></i>&nbsp;复制
				</button>
				<button type="button" class="btn btn-success btn-sm btnWidth"
					id="returnBtn">
					<i class="glyphicon glyphicon-retweet" style="color: #FFFFFF"></i>&nbsp;返回
				</button>
			</div>


			<div>
				<button type="button" class="btn btn-success btn-xs"
					id="insertRowBtn" onclick="insertRow()">
					<i class="glyphicon glyphicon-plus" style="color: #FFFFFF"></i>&nbsp;添加项目
				</button>
			</div>

			<table id="data_tabs" class="table"
				style="margin-top: 20px; margin-left: 20px; width: 98%;">
				<thead>
					<tr>
						<th width="5%" class="hidden">ID</th>
						<th width="20%">项目</th>
						<th width="7%">类型</th>
						<th width="7%">数量</th>
						<th width="7%">抵扣方式</th>
						<th width="7%">预付金额</th>
						<th width="15%">操作</th>
					</tr>
				</thead>
				<tbody>
					<core:forEach items="${mealLineResultList}" var="list">
						<tr>
							<td class="text-left hidden">${list.id}</td>
							<td class="text-left">${list.shortName}</td>
							<td class="text-left"><core:choose>
									<core:when test="${list.ticketType=='COUPON'}">
										<%=CodeTextUtils.getText("Ticket.TicketType", "COUPON")%>
									</core:when>
									<core:when test="${list.ticketType=='REDEMPTION'}">
										<%=CodeTextUtils.getText("Ticket.TicketType", "REDEMPTION")%>
									</core:when>
								</core:choose></td>
							<td class="text-right">${list.numberOfUse}</td>
							<td class="text-left"><core:choose>
									<core:when test="${list.hasVerificationCode=='Y'}">
										抵扣码
									</core:when>
									<core:when test="${list.hasVerificationCode=='N'}">
										无
									</core:when>
								</core:choose></td>
							<td class="text-right"><fmt:formatNumber
									value="${list.prepayAmount}" pattern="##,###.##"
									minFractionDigits="1"></fmt:formatNumber></td>
							<td class="text-left"><core:choose>
									<core:when test="${mealModel.status != '1'}">
										<button type="button" class="btn btn-success btn-xs btnLength"
											onClick="editMealLineInfo('${list.id}')">修改</button>
										<button type="button" class="btn btn-success btn-xs btnLength"
											onClick="removeMealLine('${list.id}')">删除</button>
									</core:when>
									<core:when test="${mealModel.status == '1'}">
										<button type="button" class="btn btn-success btn-xs btnLength"
											disabled onClick="editMealLineInfo('${list.id}')">修改</button>
										<button type="button" class="btn btn-success btn-xs btnLength"
											disabled onClick="removeMealLine('${list.id}')">删除</button>
									</core:when>
								</core:choose></td>
						</tr>
					</core:forEach>
				</tbody>
			</table>
		</form:form>
	</div>
</body>
</html>